package com.kltn.zigo;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Locale;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SearchMapActivity extends FragmentActivity {
	Button btnSeach;
	EditText edtAddressSearch;
    private GoogleMap googleMap;


	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_map);
		ActionBar actionBar = getActionBar();		
		actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#b51d21")));
		btnSeach = (Button)findViewById(R.id.btnLocation);
		edtAddressSearch = (EditText)findViewById(R.id.edtAddressSearch);
		try {
            // Loading map
            //initilizeMap();
			if (googleMap == null) {
	            googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
	 
	            // check if map is created successfully or not
	            if (googleMap == null) {
	                Toast.makeText(getApplicationContext(),
	                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
	                        .show();
	            }
	        }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
		
		 
		final Geocoder geoCoder = new Geocoder(this, Locale.getDefault()); 
		final String map = "http://maps.google.co.in/maps?q=" + edtAddressSearch.getText().toString(); 
		final String adderess = edtAddressSearch.getText().toString();
		
		btnSeach.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*String uri = String.format("geo:0,0?q=%s",URLEncoder.encode(edtAddressSearch.getText().toString()));
				Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
				startActivity(i);*/
				
				 try {
				        List<Address> addresses = geoCoder.getFromLocationName(edtAddressSearch.getText().toString(), 5);
				        String temp = edtAddressSearch.getText().toString();
				        
				        if (addresses.size() > 0) {

				            Double lat = (double) (addresses.get(0).getLatitude());
				            Double lon = (double) (addresses.get(0).getLongitude());

				            Log.d("lat-long", "" + lat + "......." + lon);
				            final LatLng user = new LatLng(lat, lon);
				            /*used marker for show the location */
				            /*MarkerOptions hamburg = new MarkerOptions()
				                    .position(user)
				                    .title(adderess)
				                    .icon(BitmapDescriptorFactory
				                            .fromResource(R.drawable.marker));
				            googleMap.addMarker(hamburg);*/
				            Marker hamburg = googleMap.addMarker(new MarkerOptions()
		                    .position(user)
		                    .title(adderess)
		                    .icon(BitmapDescriptorFactory
		                            .fromResource(R.drawable.marker)));
				            // Move the camera instantly to hamburg with a zoom of 15.
				            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(user, 15));

				            // Zoom in, animating the camera.
				            googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
				        }
				    } catch (IOException e) {
				        e.printStackTrace();
				    }
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.search_map, menu);
		return true;
	}

}
